package com.bw.module_staff.view

import android.util.Log
import android.widget.PopupMenu
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_staff.R
import com.bw.module_staff.databinding.ActivityAddStaffBinding
import com.bw.module_staff.entity.StaffEntity
import com.bw.module_staff.viewmodel.StaffViewModel
import dagger.hilt.android.AndroidEntryPoint
/**
 * 工单-智慧园区-人员管理
 */

@Route(path = RouterPath.PATH_ADD_STAFF_ACTIVITY)
class AddStaffActivity : BaseMvvmActivity<ActivityAddStaffBinding>() {
    private val vm by lazy { ViewModelProvider(this)[StaffViewModel::class.java] }
    private var departmentId: Int = 0

    override fun initView() {
        mDataBinding.imageView.setOnClickListener { finish() }
        mDataBinding.no.setOnClickListener { finish() }
        mDataBinding.departmentIv.setOnClickListener {
            val popupMenu = PopupMenu(this, it)
            popupMenu.menuInflater.inflate(R.menu.department_menu, popupMenu.menu)

            popupMenu.setOnMenuItemClickListener { item ->
                when (item.itemId) {
                    R.id.item1 -> {
                        departmentId = 16
                        mDataBinding.department.text = "研发部"
                        true
                    }
                    R.id.item2 -> {
                        departmentId = 17
                        mDataBinding.department.text = "技术部"
                        true
                    }
                    R.id.item3 -> {
                        departmentId = 18
                        mDataBinding.department.text = "市场部"
                        true
                    }
                    else -> false
                }
            }

            popupMenu.show()
        }

        mDataBinding.positionIv.setOnClickListener {
            val popupMenu = PopupMenu(this, it)
            popupMenu.menuInflater.inflate(R.menu.position_menu, popupMenu.menu)

            popupMenu.setOnMenuItemClickListener { item ->
                when (item.itemId) {
                    R.id.item1 -> {
                        mDataBinding.position.text = "总经理"
                        true
                    }
                    R.id.item2 -> {
                        mDataBinding.position.text = "销售经理"
                        true
                    }
                    R.id.item3 -> {
                        mDataBinding.position.text = "财务主管"
                        true
                    }
                    R.id.item4 -> {
                        mDataBinding.position.text = "人事行政主管"
                        true
                    }
                    else -> false
                }
            }

            popupMenu.show()
        }

        mDataBinding.yes.setOnClickListener {
            vm.addStaff(
                StaffEntity(
                    "",
                    departmentId,
                    mDataBinding.phone.text.toString() + "@163.com",
                    0,
                    mDataBinding.name.text.toString(),
                    0
                )
            )
        }

        vm.addStaffSuccess.observe(this) {
            Log.i("staff", "initData: ")
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun observeDataChange() {

    }

    override fun getLayoutId(): Int = R.layout.activity_add_staff

}